package cn.hcbycloud.gateway.service;

import cn.hcbycloud.common.core.utils.UrlPathMatcherUtils;
import cn.hutool.core.collection.CollUtil;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.List;

/**
 * @author gaoh
 * @desc 网关认证鉴权信息初始化配置接口
 * @date 2022年01月30日 下午 11:47
 */

public abstract class AbsGatewayInterceptPathService {

    /**
     * 忽略鉴权认证的URL
     * @return
     */
    protected abstract List<String> ignoreAuthUrls();

    /**
     * 匹配请求的URL是否为忽略鉴权的请求
     * @param request
     * @return
     */
    public boolean isIgnoreAuthUrl(ServerHttpRequest request) {
        List<String> ignoreAuthUrls = ignoreAuthUrls();
        return pathMath(request, ignoreAuthUrls);
    }

    /**
     * URL路径匹配方法
     * @param request
     * @param registerUrls
     * @return
     */
    private boolean pathMath(ServerHttpRequest request, List<String> registerUrls) {
        if (CollUtil.isNotEmpty(registerUrls)) {
            for (String registerUrl : registerUrls) {
                if (UrlPathMatcherUtils.matchUrlSuffix(registerUrl, request.getURI().getPath())) {
                    return true;
                }
            }
        }
        return false;
    }
}